/*
 * @(#)SpringObjectFactory.java $version 2012-3-2
 * 
 * Copyright 2011 RogueDevelopment Gruop. All rights Reserved.
 * ROGUEDEVELOPMENT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package com.nadostar.extensions.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author LEESUNG
 * 
 */
public class SpringObjectFactory implements ObjectFactory {
	Log log = LogFactory.getLog(SpringObjectFactory.class);

	private static ObjectFactory objectFactory;

	/**
	 * Instantiates a new spring object factory.
	 */
	private SpringObjectFactory() {
	}

	/**
	 * Gets the single instance of SpringObjectFactory.
	 * 
	 * @return single instance of SpringObjectFactory
	 */
	public static ObjectFactory getInstance() {
		if (objectFactory == null) {
			objectFactory = new SpringObjectFactory();
		}

		return objectFactory;
	}

	/**
	 * @see com.nhncorp.lucy.common.util.ObjectFactory#getObject(java.lang.String)
	 *      {@inheritDoc}
	 */
	public <T> T getObject(String objectname) {
		try {
			return (T) SpringBeanFactory.getBean(objectname);
		} catch (Exception e) {
			log.debug("Cannot find bean-definition for '" + objectname
					+ "'. Trying to create Object via Reflection...");
		}
		return (T) ReflectionUtil.createInstance(objectname);
	}

	/**
	 * @see com.nhncorp.lucy.common.util.ObjectFactory#getObject(java.lang.String,
	 *      java.lang.Object[]) {@inheritDoc}
	 */
	public <T> T getObject(String objectname, Object[] arguments) {
		try {
			return (T) SpringBeanFactory.getBean(objectname);
		} catch (Exception e) {
			log.debug("Cannot find bean-definition for '" + objectname
					+ "'. Trying to create Object via Reflection...");
		}
		return (T) ReflectionUtil.createInstance(objectname, arguments);
	}
}
